package com.qf.lanzhou.blog.api.dao.Impl;

import com.qf.lanzhou.blog.api.dao.NotificationDao;
import com.qf.lanzhou.blog.api.pojo.entity.Notification;
import com.qf.lanzhou.blog.api.pojo.entity.Page;
import com.qf.lanzhou.blog.api.util.BeanProcessor;
import com.qf.lanzhou.blog.api.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 是柠檬甜
 * @ClassName NotificationDaoImpl
 * @description 通知数据库接口实现类
 * @date 2020/12/16 9:34
 * @modifier 是柠檬甜
 * @date 2020-12-16 9:34
 * @Version V1.0
 */
public class NotificationDaoImpl implements NotificationDao {
    private static QueryRunner queryRunner;
    static {
        queryRunner=new QueryRunner(DruidUtil.getDataSource());
    }
    @Override
    public List selectAll() {
        String sql="SELECT * FROM t_notification ;";
        BeanListHandler<Notification> NotificationList = BeanProcessor.getBeanListHandler(Notification.class);
        List<Notification> query=null;
        try {
            query = queryRunner.query(sql, NotificationList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public List select() {
        String sql="SELECT * FROM t_notification where notification_status=1;";
        BeanListHandler<Notification> NotificationList = BeanProcessor.getBeanListHandler(Notification.class);
        List<Notification> query=null;
        try {
            query = queryRunner.query(sql, NotificationList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public Boolean updateAll() {
        String sql="UPDATE t_notification SET notification_status=0;";
        int update=0;
        try {
            update = queryRunner.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (update==0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Boolean update(Long id) {
        Object[] obj={id};
        String sql="UPDATE t_notification SET notification_status=1 WHERE notification_id=?;";
        int update=0;
        try {
            update = queryRunner.update(sql,obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (update==0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Boolean insert(String userName, String content) {
        Object[] obj={userName,content};
        String sql="INSERT INTO t_notification(user_name,notification_content) VALUES (?,?);";
        int insert=0;
        try {
            insert = queryRunner.update(sql,obj);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (insert==0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List selectPage(Page page) {
        String sql = "SELECT * FROM t_notification LIMIT ?,?;";
        Object[] parm = {page.getStartRow(),page.getPageSize()};
        BeanListHandler handler = BeanProcessor.getBeanListHandler(Notification.class);
        try {
            List query = (List) queryRunner.query(sql, handler, parm);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long selectCount() {
        String sql ="select count(*) from t_notification;";
        ScalarHandler<Long> handler = new ScalarHandler<>();
        try {
            Long query = queryRunner.query(sql, handler);
            return  query;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }    }
}
